const appInstance = getApp();
var url = appInstance.globalData.url;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 21,
    currentSwiper: 0,
    classifyList: [],
    loadding: false,
    pageCurrent: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      currentTab: options.id
    })

    this.OnClassifyList();
    this.getVideoJq();
  },
  //获取各个分类视频
  getVideoJq() {
    var that = this;
    wx.request({
      url: url + '/video/list',
      data: {
        category_id: that.data.currentTab
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        const tempData = that.data.classifyList;
        console.log(tempData)
        tempData.forEach(item => {
          console.log(item.videoList)
          if (item.id == that.data.currentTab) {
            item.videoList = res.data.data
          }
        })
        tempData.map(item => {
          console.log(item)
          return item.videoList
        })
        that.setData({
          classifyList: tempData
        })

      }
    })
  },
  //分类列表
  OnClassifyList() {
    var that = this;
    wx.request({
      url: url + '/category/list',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log('classifyList', res.data.data)
        that.setData({
          classifyList: res.data.data,
        }, function() {
          that.setSwiperIndex()
        })
      }
    })
  },

  setSwiperIndex() {
    this.data.classifyList.forEach((item, index) => {
      if (this.data.currentTab == item.id) {
        this.setData({
          currentSwiper: index
        })
      }
    })
  },
  //滑动切换
  swiperTab(e) {
    var that = this;
    var index = e.detail.current;
    var id = this.data.classifyList[index].id;
    that.setData({
      currentTab: id,
      currentSwiper: index,
    }, function() {
      that.getVideoJq();
    });
  },
  //点击切换

  clickTab: function(e) {
    var that = this;
    var current = e.target.dataset.current;
    if (this.data.currentTab === current) {
      return false;
    } else {
      that.setData({
        currentTab: current
      })
      that.setSwiperIndex();
    }
    this.getVideoJq();
  },
  //跳转到详情
  videoDetail(e) {
    console.log(e)
    var Id = e.currentTarget.id;
    wx.navigateTo({
      url: './videoDetail/videoDetail?id=' + Id
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    that.setData({
      loadding: true
    })
    wx.request({
      url: url + '/video/list',
      method: 'POST',
      data: {
        page: that.data.pageCurrent,
        size: 4
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        that.setData({
          loadding: false
        })
        if (res.data.data.length !== 0) {
          var list = that.data.classifyList;
          list.forEach(item => {
            if (that.data.currentTab == item.id) {
              item.videoList = item.videoList.concat(res.data.data)
            }
          })
          // console.log(list)
          that.setData({
            classifyList:list,
            pageCurrent: that.data.pageCurrent + 1
          })





        }



      }
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})